package com.test.redis.controller;

import com.test.redis.base.Result;
import com.test.redis.dto.ReservationRecordDTO;
import com.test.redis.dto.ReservationRequest;
import com.test.redis.service.ReservationBitMapService;
import com.test.redis.service.ReservationService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 会议室预定控制器
 */
@RestController
@RequestMapping("/api/reservation")
@Validated
@Slf4j
public class ReservationController {
    
    private final ReservationService reservationService;
    
    public ReservationController(ReservationService reservationService) {
        this.reservationService = reservationService;
    }
    
    /**
     * 预定会议室
     */
    @PostMapping("/reserve")
    public Result<ReservationRecordDTO> reserve(@Valid @RequestBody ReservationRequest request,
                                                @RequestHeader("X-User-Id") Long userId,
                                                @RequestHeader("X-User-Name") String userName) {
        ReservationRecordDTO result = reservationService.reserve(request, userId, userName);
        return Result.success(result);
    }
    
    /**
     * 取消预定
     */
    @PostMapping("/cancel/{reservationId}")
    public Result<Void> cancel(@PathVariable Long reservationId,
                              @RequestHeader("X-User-Id") Long userId) {
        reservationService.cancelReservation(reservationId, userId);
        return Result.success();
    }
    
    /**
     * 查询我的预定记录
     */
    @GetMapping("/my-reservations")
    public Result<List<ReservationRecordDTO>> getMyReservations(@RequestHeader("X-User-Id") Long userId) {
        List<ReservationRecordDTO> reservations = reservationService.getUserReservations(userId);
        return Result.success(reservations);
    }
    
    /**
     * 查询会议室可用时间段
     */
    @GetMapping("/available-slots/{roomId}")
    public Result<List<ReservationBitMapService.TimeSlot>> getAvailableSlots(
            @PathVariable Long roomId,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) {
        List<ReservationBitMapService.TimeSlot> slots = reservationService.getAvailableSlots(roomId, date);
        return Result.success(slots);
    }
}

